package de.clapp.backend.requests;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import de.clapp.backend.clublist.ClublistModel;
import de.clapp.backend.database.Conn;
import de.clapp.backend.database.Queries;

public class UpdateClubIPRequest implements ServerRequest
{
    private static final long serialVersionUID = 7444997945061054406L;
    
    private final String ip, port;
    private final int clubID;
    
    public UpdateClubIPRequest(final int clubID, final String ip, final String port)
    {
        this.ip = ip;
        this.port = port;
        this.clubID = clubID;
        System.out.println(this.ip + " : " + port);
    }
    
    @Override
    public Type getReturnType()
    {
        // TODO Auto-generated method stub
        return Object.class;
    }
 
    @Override
    public Object getItem()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setItem(Object item)
    {
        // TODO Auto-generated method stub
        
    }

    @Override
    public Object execute()
    {
        Connection con = Conn.getInstance().serverCon;
        PreparedStatement pstmt = null;
       
        try
        {
            // Aktualisieren des Ports
            pstmt = con.prepareStatement(Queries.UPDATE_CLUB_PORT);
            pstmt.setString(1, this.port);
            pstmt.setInt(2, this.clubID);
            pstmt.execute();
            con.commit();

            // Aktualisieren der IP
            pstmt = con.prepareStatement(Queries.UPDATE_CLUB_IP);
            pstmt.setString(1, this.ip);
            pstmt.setInt(2, this.clubID);
            pstmt.execute();
            con.commit();
            
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(pstmt != null)
            {
                try
                {
                    pstmt.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        ClublistModel.getI().refresh();
        return new Object();
    }

}
